package com.demo.datastructure.binarySearch;

/**
 * @description: 二分法
 * @author: ljr
 * @time: 2021/5/11 21:05
 */
public class BinarySearch2 {

    public static int bsearch(int[] a, int n, int value) {
          int low = 0;
          int high = n-1;
          while(low <= high){
             int mid1 = (high-low) >> 1;
             int mid = low+mid1;
             if(a[mid] == value){
                 return mid;
             }else if(a[mid] > value){
                 high = mid-1;
             }else{
                 low = mid+1;
             }
          }
          return 0;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4,5,6,7,8};
        System.out.print(BinarySearch2.bsearch(nums,8,3));
    }

}
